<template>
  <div class="expense-category-container">
    <div class="header">
      <div class="search-bar">
        <el-input
          v-model="searchQuery"
          placeholder="请输入类别名称"
          clearable
          style="width: 200px; margin-right: 10px"
        />
        <el-select
          v-model="selectedCategory"
          placeholder="分类"
          clearable
          style="width: 120px; margin-right: 10px"
        >
          <el-option label="全部" value="" />
          <el-option label="未分类" value="unclassified" />
          <el-option label="费用支出" value="expense" />
          <el-option label="薪酬支出" value="salary" />
          <el-option label="税费支出" value="tax" />
          <el-option label="其他支出" value="other" />
        </el-select>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </div>
      <div class="action-buttons">
        <el-button type="primary" @click="handleAdd">
          <el-icon><plus /></el-icon>新增
        </el-button>
        <el-button @click="handleBatchEnable">
          <el-icon><check /></el-icon>启用
        </el-button>
        <el-button @click="handleBatchDisable">
          <el-icon><close /></el-icon>禁用
        </el-button>
        <el-button @click="handleRefresh">
          <el-icon><refresh /></el-icon>刷新
        </el-button>
      </div>
    </div>

    <div class="main-content">
      <div class="category-sidebar">
        <el-tree
          :data="categoryTree"
          node-key="id"
          default-expand-all
          highlight-current
          :props="treeProps"
          @node-click="handleTreeNodeClick"
        />
      </div>

      <div class="category-list">
        <el-table :data="categoryData" border style="width: 100%">
          <el-table-column type="selection" width="55" />
          <el-table-column label="操作" width="120">
            <template #default>
              <el-button type="text" size="small" @click="handleEdit">
                <el-icon><edit /></el-icon>
              </el-button>
              <el-button type="text" size="small" @click="handleDelete">
                <el-icon><delete /></el-icon>
              </el-button>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="类别名称" />
          <el-table-column prop="category" label="分类" />
          <el-table-column prop="status" label="状态" width="120">
            <template #default="{ row }">
              <el-switch
                v-model="row.status"
                active-color="#409EFF"
                inactive-color="#DCDFE6"
                :active-value="true"
                :inactive-value="false"
                @change="handleStatusChange(row)"
              />
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" />
        </el-table>

        <div class="pagination">
          <el-pagination
            :current-page="currentPage"
            :page-size="pageSize"
            :pager-count="5"
            layout="total, prev, pager, next"
            :total="total"
            @current-change="handlePageChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import {
  Plus,
  Check,
//   Close,
  Refresh,
  Edit,
  Delete
} from '@element-plus/icons-vue'

// 树状分类数据
const categoryTree = ref([
  {
    id: 'all',
    label: '全部',
    children: [
      { id: 'unclassified', label: '未分类' },
      { id: 'expense', label: '费用支出' },
      { id: 'salary', label: '薪酬支出' },
      { id: 'tax', label: '税费支出' },
      { id: 'other', label: '其他支出' }
    ]
  }
])

const treeProps = {
  children: 'children',
  label: 'label'
}

// 表格数据
const categoryData = ref([]) // 空数组，不填充数据
const currentPage = ref(1)
const pageSize = ref(50)
const total = ref(0)

// 搜索条件
const searchQuery = ref('')
const selectedCategory = ref('')

// 操作函数
const handleTreeNodeClick = (data) => {
  console.log('选择分类节点:', data)
  // 这里可以添加加载对应分类数据的逻辑
}

const handleSearch = () => {
  console.log('执行搜索:', {
    query: searchQuery.value,
    category: selectedCategory.value
  })
  // 这里可以添加搜索逻辑
}

const handleAdd = () => {
  console.log('新增类别')
}

const handleEdit = () => {
  console.log('编辑类别')
}

const handleDelete = () => {
  console.log('删除类别')
}

const handleStatusChange = (row) => {
  console.log('状态变更:', row.status, row)
}

const handleBatchEnable = () => {
  console.log('批量启用')
}

const handleBatchDisable = () => {
  console.log('批量禁用')
}

const handleRefresh = () => {
  console.log('刷新数据')
}

const handlePageChange = (page) => {
  currentPage.value = page
  console.log('页码变更:', page)
  // 这里可以添加分页加载数据的逻辑
}
</script>

<style scoped>
.expense-category-container {
  padding: 20px;
  background-color: #fff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-bar {
  display: flex;
  align-items: center;
}

.action-buttons {
  display: flex;
  gap: 10px;
}

.main-content {
  display: flex;
  min-height: calc(100vh - 180px);
}

.category-sidebar {
  width: 200px;
  margin-right: 20px;
  border-right: 1px solid #ebeef5;
  padding-right: 10px;
}

.category-list {
  flex: 1;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-tree {
  background-color: transparent;
}

.el-tree-node__content {
  height: 36px;
}
</style>